package com.example.demo.cmiemail;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * Created by Hy on 2018/8/6.
 */
@Component
@PropertySource("classpath:application.properties")
public class EmailHelper {
private Logger log = LoggerFactory.getLogger(getClass());
public void sendCodeToEmail(String emailAddr,String content) throws EmailException {
    if(SystemUtil.HOST_NAME == null){
        log.info("HOST_NAME 注入失败");
    }
    //获取发送邮箱对象
    HtmlEmail htmlEmail = initHttpEmail();
    //设置目标邮箱
    htmlEmail.addTo(emailAddr);
    //设置内容
    htmlEmail.setMsg(contentHelper(content));
    //发送邮件
    ExecNewThreadFunctionHelper.sendEmail(htmlEmail);
}

    //把传入的字符串加到基础的字符串中去  《《《？？？？干吗用的没懂》》》
    private String contentHelper(String str) {
    return SystemUtil.BASE_CONTENT.replace("*",str);
    }

    /**
     * 初始化简单的email客户端
     * @return
     * @throws EmailException
     */
    private static HtmlEmail initHttpEmail() throws EmailException {
    HtmlEmail htmlEmail = new HtmlEmail();
    htmlEmail.setHostName(SystemUtil.HOST_NAME);//指定要使用的邮件服务器
    htmlEmail.setAuthentication(SystemUtil.USER,SystemUtil.PASSWORD);
    htmlEmail.setCharset(SystemUtil.CHARSET);
    htmlEmail.setFrom(SystemUtil.FROM, SystemUtil.FROM_NAME);
    htmlEmail.setSubject(SystemUtil.TITLE);
    htmlEmail.setSocketTimeout(60 * 1000);
    htmlEmail.setSocketConnectionTimeout(60 * 1000);
    return htmlEmail;
    }

    /**
     * CRM工单分派时发送邮件
     * @param emailAddr
     * @param title
     * @param content
     */
    public void CrmSendMsgToEmail(String emailAddr, String title, String content) throws EmailException {
        if(SystemUtil.HOST_NAME == null){
            log.info("HOST NAME注入失败");
        }
        //获取发送邮箱对象
        HtmlEmail htmlEmail = initHttpEmail();
        //设置目标邮箱
        String[] str = emailAddr.split(",");
        for (int i=0;i<str.length;i++){
            String email = str[i];
            htmlEmail.addTo(email);
        }
        //设置内容
        htmlEmail.setMsg(content);
        htmlEmail.setSubject(title);
        //发送邮件
        ExecNewThreadFunctionHelper.sendEmail(htmlEmail);
    }

    /**
     * 发送内容为html格式页面的邮件
     * @param emailAddr
     * @param title
     * @param content
     */
    public void SendHtmlEmail(String emailAddr, String title, String htmlMsg) throws EmailException {
       if(emailAddr==null){return;}
       if(SystemUtil.HOST_NAME==null){
           log.info("HOST_NAME注入失败");
       }
       //获取发送邮箱对象
        HtmlEmail htmlEmail = initHttpEmail();
       //设置目标邮箱
        String[] str = emailAddr.split(",");
        for (int i=0;i<str.length;i++){
            String email = str[i];
            htmlEmail.addTo(email);
        }
        htmlEmail.setHtmlMsg(htmlMsg);
        htmlEmail.setSubject(title);
        ExecNewThreadFunctionHelper.sendEmail(htmlEmail);
    }

    public void VoiceSendMsgToEmail(String emailAddr, String title, String content) {
    }
}
